package com.mcptt.comm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.cloudptt.api.product.log.Log;

/**
 * @author SunnyCoffee
 * @date 2014-1-27
 * @version 1.0
 * @desc desc 鍏叡甯搁噺
 * 
 */
public class CommonFunc {
	private static final String TAG = "CommonFunc";
	public static final String LOCATION = "location";
	public static final String LOCATION_ACTION = "locationAction";

	public static String getDateAndTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format( new Date());
	}
	public static String getDateAndTime2() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format( new Date());
	}

	public static String getDateAndTimeSSS() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		return format.format( new Date());
	}

	public static String getDate() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format( new Date());
	}

	/**
	 * 鍙戦�佹秷鎭椂锛岃幏鍙栧綋鍓嶄簨浠�
	 * 
	 * @return 褰撳墠鏃堕棿
	 */
	public static String getFormatDate(String strDate) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long date = Long.valueOf(strDate)*1000;
		return format.format(new Date(date));
	}

	public static long getIntervalDate(String strDate) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date;
		try {
			date = format.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return 0;
		}
		return date.getTime() / 1000;
	}

	public static boolean isExpire(String expireData) 
	{
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
		try 
		{
			Date    curDate    =   new    Date(System.currentTimeMillis());
			Date data = df.parse(expireData);
			if (curDate.getTime() <= data.getTime()) {
				return true;
			} 
			else
			{
				return false;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return false;
	}

	public static String unicode2String(String unicodeStr) {
		StringBuffer sb = new StringBuffer();
		String tmp = null;
		while(unicodeStr.length()>=4)
		{

			tmp = unicodeStr.substring(0, 4) ;
			unicodeStr = unicodeStr.substring(4, unicodeStr.length());
			char c = (char) Integer.parseInt(tmp, 16);
			sb.append(c);
		}


		return sb.toString();
	}

	public static String convertStreamToString(InputStream is) {   
		/* 
		 * To convert the InputStream to String we use the BufferedReader.readLine() 
		 * method. We iterate until the BufferedReader return null which means 
		 * there's no more data to read. Each line will appended to a StringBuilder 
		 * and returned as String. 
		 */ 
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));   
		StringBuilder sb = new StringBuilder();   

		String line = null;   
		try {   
			while ((line = reader.readLine()) != null) {   
				Log.debug(CommonFunc.class, "convertStreamToString line = " + line);
				sb.append(line + "\n");   
			}   
		} catch (IOException e) {   
			e.printStackTrace();   
		} finally {   
			try {   
				is.close();   
			} catch (IOException e) {   
				e.printStackTrace();   
			}   
		}   

		return sb.toString();   
	} 

	public static String convertStreamToString(InputStream is,String keyNoStr) {   
		/* 
		 * To convert the InputStream to String we use the BufferedReader.readLine() 
		 * method. We iterate until the BufferedReader return null which means 
		 * there's no more data to read. Each line will appended to a StringBuilder 
		 * and returned as String. 
		 */ 
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));   
		StringBuilder sb = new StringBuilder();   

		String line = null;   
		try {   
			while ((line = reader.readLine()) != null) {   
				Log.debug(CommonFunc.class, "convertStreamToString line = " + line);
				if(!line.contains(keyNoStr)
						&& !line.contains("effective_caller_id_name")
						&& !line.contains("OK"))
				{
					sb.append(line + "\n");   
				}
			}   
		} catch (IOException e) {   
			e.printStackTrace();   
		} finally {   
			try {   
				is.close();   
			} catch (IOException e) {   
				e.printStackTrace();   
			}   
		}   

		return sb.toString();   
	} 
}
